﻿/*========================== 
 * @author 郑金泉
 * @desc 实体Decimal字段类
 * ========================= */
using System;
using System.Collections.Generic;
using System.Text;

namespace Obsidian.Edm
{
    public class DecimalField : ModelField, IModelField
    {

        public override FieldType Type
        {
            get { return FieldType.Decimal; }
        }

        private decimal _value;
        /// <summary>
        /// 获取值
        /// </summary>
        public decimal Value
        {
            get
            {
                if (_isNull)
                    throw new Exception("空值状态无法获得该属性值");
                return this._value;
            }
        }

        /// <summary>
        /// 获取值
        /// </summary>
        public object OValue
        {
            get
            {
                if (_isNull) return null;
                else return _value;
            }
        }

        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public IModel Set(decimal value)
        {
            if (!base._assigned)
                base._assigned = true;
            this._isNull = false;
            this._value = value;
            return base.Model;
        }

        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public IModel Set(object value)
        {
            if (value == null)
            {
                return this.SetNull();
            }
            else
            {
                decimal decimalVal = Convert.ToDecimal(value);
                return this.Set(decimalVal);
            }
        }

        /// <summary>
        /// 设置值
        /// </summary>
        public bool TrySet(object value)
        {
            if (value == null)
            {
                this.SetNull();
                return true;
            }

            try
            {
                this.Set(Convert.ToDecimal(value));
                return true;
            }
            catch
            {
                this.SetNull();
                return false;
            }

        }

        /// <summary>
        /// 设置为空值状态
        /// </summary>
        public IModel SetNull()
        {
            this._value = -1;
            this._isNull = true;
            return base.Model;
        }

        public DecimalField(IModel model)
        {
            this.InitField(model);
        }

        public DecimalField(IModel model, string dbFieldName)
        {
            this.InitField(model, dbFieldName);
        }

        public DecimalField(IModel model, string dbFieldName, string alias)
        {
            this.InitField(model, dbFieldName, alias);
        }

        public DecimalField(IModel model, string dbFieldName, string alias, decimal defaultValue)
        {
            this.InitField(model, dbFieldName, alias);
            this._value = defaultValue;
            this._isNull = false;
        }

        public string ToString()
        {
            return _isNull ? null : _value.ToString();
        }
    }
}
